package com.elinkway.infinitemovies.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 公共SharedPreferences 类
 *
 * @author zhangshuo
 */
public class SharePreferenceManager {

    private final static String COMMON_SP_NAME = "commonsp";

    public final static String SHARE_GUIDE_FLAG = "shareguideflag";
    public final static String SUBCHANNEL_POST_FLAG = "shareguideflag";
    public final static String LITE_APP_MD5 = "liteappmd5";
    public final static String IS_FIRST_INVOKE_LITE = "isfirstinvokelite";
    public final static String EXIT_AD_ID = "exitAdId";
    public final static String EXIT_AD_TYPE = "exitAdType";
    public final static String HOME_TAB_JSON = "homeTabJson";
    public final static String LOCATION_LAT_LON = "location_lat_lon";

    public final static String PRIMEPLAYTYPE = "primeplaytype"; // 乐视源专用，首选播放方式，0：305；1：SDK；2：截流；3：关闭；4：h5
    public final static String FAILOVERPLAYTYPE = "failoverplaytype"; // 乐视源专用，首选播放失败时，次选播放方式
    public final static String LETVDOWNLOAD = "letvdownload"; // 乐视源专用，是否支持305下载，1是0否
    public final static String SEARCH_HOT_DATA = "search_hot";
    public final static String DOWNLOAD_HOT_DATA = "download_hot";
    public final static String ISROOTED = "isrooted";
    public final static String BROWSER_UA = "browser_ua";
    public final static String YIDIAN_TAG = "yiDianTag";
    public final static String YIDIAN_OPEN_NUM = "yidianOpenNum"; //启动次数

    public static void saveVaule(Context context, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(COMMON_SP_NAME, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static void saveVaule(Context context, String key, boolean value) {
        SharedPreferences sp = context.getSharedPreferences(COMMON_SP_NAME, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static void saveVaule(Context context, String key, int value) {
        SharedPreferences sp = context.getSharedPreferences(COMMON_SP_NAME, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static boolean getVaule(Context context, String key) {
        return getVaule(context, key, false);
    }

    private static boolean getVaule(Context context, String key, boolean defValue) {
        boolean flag = false;
        try {
            SharedPreferences sp =
                    context.getSharedPreferences(COMMON_SP_NAME, Context.MODE_PRIVATE);
            flag = sp.getBoolean(key, defValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 获取String类型数据
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static String getVaule(Context context, String key, String defValue) {
        String flag = defValue;
        try {
            SharedPreferences sp =
                    context.getSharedPreferences(COMMON_SP_NAME, Context.MODE_PRIVATE);
            flag = sp.getString(key, defValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
    public static int getVaule(Context context, String key, int defValue) {
        int flag = defValue;
        try {
            SharedPreferences sp =
                    context.getSharedPreferences(COMMON_SP_NAME, Context.MODE_PRIVATE);
            flag = sp.getInt(key, defValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}
